<template>
  <el-form ref="formRef" :model="form" :inline="true" class="list-search">
    <el-button type="primary" @click="edit">新增</el-button>
    <el-button type="danger" @click="batchRemove">批量删除</el-button>
    <div class="space"></div>
    <el-form-item label="姓名">
      <el-input v-model="form.name" placeholder="输入姓名"></el-input>
    </el-form-item>
    <el-form-item label="状态">
      <el-select v-model="form.status" placeholder="选择状态">
        <el-option label="启用" value="1"></el-option>
        <el-option label="禁用" value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="search">查询</el-button>
      <el-button @click="formRef.resetFields()">重置</el-button>
    </el-form-item>
  </el-form>
  <el-container>
    <el-aside class="list-tree">
      <el-tree accordion :data="tree" :props="defaultProps"></el-tree>
    </el-aside>
    <el-container direction="vertical">
      <el-table highlight-current-row v-loading="loading" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="name" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" @click="remove(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          background
          :page-sizes="[20, 50, 100, 500]"
          v-model:pageSize="form.pageSize"
          v-model:currentPage="form.pageNo"
          layout="total, sizes, pager"
          :total="total">
      </el-pagination>
    </el-container>
  </el-container>

  <el-dialog width="550px" :title="dialogFormTitle" v-model="dialogFormVisible">
    <DemoForm :id="dialogFormId" @close="dialogFormVisible = false" v-if="dialogFormVisible"></DemoForm>
  </el-dialog>
</template>

<script>
import {reactive, watch, toRefs, onMounted, ref} from 'vue'
import http from '../../utils/http'
import {ElMessage, ElMessageBox} from 'element-plus'
import DemoForm from './form.vue'

export default {
  components: {DemoForm},
  setup() {
    const state = reactive({
      loading: false,
      form: {
        name: null,
        status: null,
        pageNo: 1,
        pageSize: 20,
      },
      list: null,
      total: 0,
      dialogFormId: null,
      dialogFormTitle: '',
      dialogFormVisible: false,
      multipleSelection: null,
      tree: [{
        label: '一级 1',
        children: [{
          label: '二级 1-1',
          children: [{
            label: '三级 1-1-1'
          }]
        }]
      }, {
        label: '一级 2',
        children: [{
          label: '二级 2-1',
          children: [{
            label: '三级 2-1-1'
          }]
        }, {
          label: '二级 2-2',
          children: [{
            label: '三级 2-2-1'
          }]
        }]
      }, {
        label: '一级 3',
        children: [{
          label: '二级 3-1',
          children: [{
            label: '三级 3-1-1'
          }]
        }, {
          label: '二级 3-2',
          children: [{
            label: '三级 3-2-1'
          }]
        }]
      }],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    })

    const fetchList = async () => {
      state.loading = true
      const result = await http('/api/users', {data: state.form})
      state.list = result.data.list
      state.total = result.data.total
      state.loading = false
    }
    watch(() => [state.form.pageNo, state.form.pageSize], () => fetchList())
    onMounted(fetchList)

    const search = () => {
      state.pageNo = 1
      fetchList()
    }
    const changeStatus = async (row) => {
      state.loading = true
      const result = await http.patch('/api/status', {id: row.id})
      state.loading = false
      if (!result.success)
        return
      ElMessage.success('操作成功')
    }
    const edit = (row) => {
      state.dialogFormTitle = row ? '编辑' : '新增'
      state.dialogFormId = row ? row.id : null
      state.dialogFormVisible = true
    }
    const remove = () => {
      ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ElMessage.success({
          type: 'success',
          message: '删除成功!'
        })
        fetchList()
      }, () => {
      })
    }
    const batchRemove = () => {
      if (state.multipleSelection == null)
        return
      ElMessageBox.confirm('确定删除选中的记录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ElMessage.success({
          type: 'success',
          message: '删除成功!'
        })
        fetchList()
      }, () => {
      })
    }
    const handleSelectionChange = (value) => {
      const idList = value.map(v => v.id)
      state.multipleSelection = idList.length > 0 ? idList.join(',') : null
    }

    const formRef = ref(null)
    return {
      ...toRefs(state),
      search,
      changeStatus,
      edit,
      remove,
      batchRemove,
      formRef,
      handleSelectionChange,
    }
  },
}
</script>

<style scoped>

</style>
